package cn.csc.module.product.controller.admin.shopcart;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.shopcart.vo.*;
import cn.csc.module.product.convert.shopcart.SopCartItemConvert;
import cn.csc.module.product.dal.dataobject.shopcart.SopCartItemDO;
import cn.csc.module.product.service.shopcart.SopCartItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 购物车")
@RestController
@RequestMapping("/product/sop-cart-item")
@Validated
public class SopCartItemController {

    @Resource
    private SopCartItemService sopCartItemService;

    @PostMapping("/create")
    @ApiOperation("创建购物车")
    @PreAuthorize("@ss.hasPermission('product:sop-cart-item:create')")
    public CommonResult<BigInteger> createSopCartItem(@Valid @RequestBody SopCartItemCreateReqVO createReqVO) {
        return success(sopCartItemService.createSopCartItem(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新购物车")
    @PreAuthorize("@ss.hasPermission('product:sop-cart-item:update')")
    public CommonResult<Boolean> updateSopCartItem(@Valid @RequestBody SopCartItemUpdateReqVO updateReqVO) {
        sopCartItemService.updateSopCartItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除购物车")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:sop-cart-item:delete')")
    public CommonResult<Boolean> deleteSopCartItem(@RequestParam("id") BigInteger id) {
        sopCartItemService.deleteSopCartItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得购物车")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:sop-cart-item:query')")
    public CommonResult<SopCartItemRespVO> getSopCartItem(@RequestParam("id") BigInteger id) {
        SopCartItemDO sopCartItem = sopCartItemService.getSopCartItem(id);
        return success(SopCartItemConvert.INSTANCE.convert(sopCartItem));
    }

    @GetMapping("/list")
    @ApiOperation("获得购物车列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:sop-cart-item:query')")
    public CommonResult<List<SopCartItemRespVO>> getSopCartItemList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SopCartItemDO> list = sopCartItemService.getSopCartItemList(ids);
        return success(SopCartItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得购物车分页")
    @PreAuthorize("@ss.hasPermission('product:sop-cart-item:query')")
    public CommonResult<PageResult<SopCartItemRespVO>> getSopCartItemPage(@Valid SopCartItemPageReqVO pageVO) {
        PageResult<SopCartItemDO> pageResult = sopCartItemService.getSopCartItemPage(pageVO);
        return success(SopCartItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出购物车 Excel")
    @PreAuthorize("@ss.hasPermission('product:sop-cart-item:export')")
    @OperateLog(type = EXPORT)
    public void exportSopCartItemExcel(@Valid SopCartItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SopCartItemDO> list = sopCartItemService.getSopCartItemList(exportReqVO);
        // 导出 Excel
        List<SopCartItemExcelVO> datas = SopCartItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "购物车.xls", "数据", SopCartItemExcelVO.class, datas);
    }

}
